package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.IndustryMapInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntImageInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.IndustryMapInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntImageInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.IndustryMapInfo;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.MchntImageInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.*;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.realize.MchntImageUnifiedService;
import cc.rengu.igas.bomp.core.realize.NormalMchntInfoService;
import cc.rengu.igas.bomp.core.realize.impl.MchntImageUnifiedServiceImpl;
import cc.rengu.igas.bomp.core.realize.impl.NormalMchntInfoServiceImpl;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.ImageInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntApexInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntImageInfoBean;
import cc.rengu.igas.bomp.facade.enums.BompMchntNatureEnum;
import cc.rengu.igas.bomp.facade.request.WeChatActivityRateRegistRequest;
import cc.rengu.igas.bomp.facade.response.WeChatActivityRateRegistResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.bean.ActivityAdditionalInfo;
import cc.rengu.igas.channel.wechat.facade.bean.ActivityApplyInfo;
import cc.rengu.igas.channel.wechat.facade.bean.ActivityDetailInfo;
import cc.rengu.igas.channel.wechat.facade.request.ActivityRateDiscountRegistRequest;
import cc.rengu.igas.channel.wechat.facade.response.ActivityRateDiscountRegistResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信特殊费率申请
 *
 * @version 1.0
 * @date 2019/08/15
 */
public class WeChatActivityRateRegistService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WeChatActivityRateRegistRequest weChatActivityRateRegistRequest = new WeChatActivityRateRegistRequest();
        ConvertUtil.convertOutput(weChatActivityRateRegistRequest);
        return weChatActivityRateRegistRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        WeChatActivityRateRegistRequest weChatActivityRateRegistRequest = (WeChatActivityRateRegistRequest) request;
        WeChatActivityRateRegistResponse weChatActivityRateRegistResponse = new WeChatActivityRateRegistResponse();
        BizResponse<WeChatActivityRateRegistResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(weChatActivityRateRegistResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        weChatActivityRateRegistResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        weChatActivityRateRegistResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        NormalMchntInfoService normalMchntInfoService = new NormalMchntInfoServiceImpl();
        MchntBaseInfo mchntBaseInfo = normalMchntInfoService.checkMchntStatus(weChatActivityRateRegistRequest.getHeader().getInstId(), weChatActivityRateRegistRequest.getMchntNo());
        //先置为非正式，用于将数据直接入库
        mchntBaseInfo.setMchntStatus(MchntStatusEnum.UNNORMAL.getStatus());

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
        if (null == wechatMchntInfo) {
            rglog.error("无微信商户信息.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "无微信产品信息");
        }
        if (StringUtil.isEmptyOrNull(wechatMchntInfo.getWechatMchntNo())) {
            rglog.error("未微信进件.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未微信进件");
        }
        if (!AppParamConstant.YES.equals(wechatMchntInfo.getRealNameFlag())) {
            rglog.error("未微信实名认证.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未微信实名认证");
        }

        checkMchntParam(weChatActivityRateRegistRequest, mchntBaseInfo);
        imageUpToWechat(weChatActivityRateRegistRequest, mchntBaseInfo, wechatMchntInfo);
        saveMchntParam(weChatActivityRateRegistRequest, mchntBaseInfo);

        ActivityRateDiscountRegistRequest activityRateDiscountRegistRequest = initActivityRateDiscountRegistRequest(weChatActivityRateRegistRequest, mchntBaseInfo, wechatMchntInfo);

        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "activityRateDiscountRegist", activityRateDiscountRegistRequest);
        if (null == result || !result.isSuccess() || null == result.getResult()) {
            rglog.error("服务调用失败.");
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ActivityRateDiscountRegistResponse activityRateDiscountRegistResponse = (ActivityRateDiscountRegistResponse) result.getResult();
        if (!DubboMethodConstant.DUBBO_SUCCESS.equals(activityRateDiscountRegistResponse.getRespCode())) {
            rglog.error("调用服务失败<{}>-<{}>", activityRateDiscountRegistResponse.getRespCode(), activityRateDiscountRegistResponse.getRespDesc());
            throw new BizException(activityRateDiscountRegistResponse.getRespCode(), activityRateDiscountRegistResponse.getRespDesc());
        }
        if (StringUtil.isEmptyOrNull(activityRateDiscountRegistResponse.getApplicationId())) {
            rglog.error("微信未返回申请单编号.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "微信未返回申请单编号");
        }

        /*保存申请单号*/
        List<MchntApexInfoBean> mchntApexInfoBeanList = new ArrayList<>();
        MchntApexInfoBean wApplicationIdMchntApexInfoBean = new MchntApexInfoBean();
        mchntApexInfoBeanList.add(wApplicationIdMchntApexInfoBean);
        wApplicationIdMchntApexInfoBean.setPropertyKey(MchntApexPropeytyEnum.APPLICATION_ID.getPropertyKey());
        wApplicationIdMchntApexInfoBean.setPropertyValue(activityRateDiscountRegistResponse.getApplicationId());

        /*申请单状态*/
        MchntApexInfoBean wActivityStatusMchntApexInfoBean = new MchntApexInfoBean();
        mchntApexInfoBeanList.add(wActivityStatusMchntApexInfoBean);
        wActivityStatusMchntApexInfoBean.setPropertyKey(MchntApexPropeytyEnum.ACTIVITY_STATUS.getPropertyKey());
        wActivityStatusMchntApexInfoBean.setPropertyValue(WechatActivityStatusEnum.APPLICATION_STATE_AUDITING.getEpayStatus());

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();
        try {
            InitParamUtil.savemchntApexInfo(mchntBaseInfo, mchntApexInfoBeanList);
            /*提交事务*/
            dbsUtil.dbsEndTransaction(true);
        } catch (Exception e) {
            rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }

        weChatActivityRateRegistResponse.setApplicationId(activityRateDiscountRegistResponse.getApplicationId());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WeChatActivityRateRegistResponse weChatActivityRateRegistResponse = (WeChatActivityRateRegistResponse) bizResponse.getResult();
        ConvertUtil.convertInput(weChatActivityRateRegistResponse);
    }

    /**
     * 检查参数
     */
    private void checkMchntParam(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest, MchntBaseInfo mchntBaseInfo) throws Exception {
        SysParam activityListSysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), SysParamEnum.ACTIVITY_LIST);
        if (null == activityListSysParam) {
            rglog.error("微信特殊费率活动列表未配置.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "微信特殊费率活动列表未配置");
        }
        if (CollectionUtils.isEmpty(weChatActivityRateRegistRequest.getMchntApexInfoBeanList())) {
            rglog.error("未上传拓展属性.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未上传拓展属性");
        }
        List<MchntApexInfoBean> activityIdMchntApexInfoBeanList = weChatActivityRateRegistRequest.getMchntApexInfoBeanList()
                .stream().filter(item -> MchntApexPropeytyEnum.WECHAT_ACTIVITY_ID.getPropertyKey().equals(item.getPropertyKey())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(activityIdMchntApexInfoBeanList)) {
            rglog.error("未上传活动ID.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未上传活动ID");
        }
        String activityId = activityIdMchntApexInfoBeanList.get(0).getPropertyValue();
        if (StringUtil.isEmptyOrNull(activityId)) {
            rglog.error("活动ID为空.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "活动ID为空");
        }
        if (!activityListSysParam.getParamValue().contains(activityId)) {
            rglog.error("活动<{}>不在配置的活动列表<{}>中.", activityId, activityListSysParam.getParamValue());
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "活动不在配置的活动列表中");
        }

        SysParam supportSubjectTypeSysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), activityId, SysParamEnum.SUPPORT_SUBJECT_TYPE.getParamKey());
        if (null == supportSubjectTypeSysParam) {
            rglog.error("未配置活动<{}>所支持的主体类型.", activityId);
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动所支持的主体类型");
        }

        //默认对公
        String publicPrivateKey = "01";
        BompMchntNatureEnum wechatType = BompMchntNatureEnum.getBompMchntNatureEnumByType(mchntBaseInfo.getMchntNature());
        if (null != wechatType) {
            //根据商户性质判断对公对私
            publicPrivateKey = wechatType.getWechatPublicPrivateKey();
        }
        IndustryMapInfoMapper industryMapInfoMapper = new IndustryMapInfoMapperImpl();
        IndustryMapInfo wechatIndustry = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(mchntBaseInfo.getIndustryType(), ChannelEnum.WXZF.getChannelType(), publicPrivateKey);
        if (null == wechatIndustry) {
            //默认使用对公再次查询，若还查无数据则失败
            wechatIndustry = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(mchntBaseInfo.getIndustryType(), ChannelEnum.WXZF.getChannelType(), "01");
            if (null == wechatIndustry) {
                rglog.error("通过行业类型查询微信行业类型映射关系失败！，instId:{},mchntNo:{},industryType:{}",
                        mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), mchntBaseInfo.getIndustryType());
                throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
            }
        }

        String supprotSubjectType = publicPrivateKey + "-" + wechatIndustry.getDestCatalogCd();
        if (!supportSubjectTypeSysParam.getParamValue().contains(supprotSubjectType)) {
            rglog.error("该商户微信主体类型+结算ID<{}>不符合该活动所支持<{}>", supprotSubjectType, supportSubjectTypeSysParam.getParamValue());
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "该活动不支持此商户微信主体类型+结算ID");
        }

        SysParam activityRateSysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), activityId, SysParamEnum.ACTIVITY_RATE.getParamKey());
        if (null == activityRateSysParam) {
            rglog.error("未配置活动<{}>的活动费率.", activityId);
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动的活动费率");
        }
        weChatActivityRateRegistRequest.setActivityRate(activityRateSysParam.getParamValue());

        SysParam materialListSysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), activityId + "-" + publicPrivateKey, SysParamEnum.MATERIAL_LIST.getParamKey());
        if (null == materialListSysParam) {
            rglog.error("未配置活动<{}>-<{}>的材料列表.", activityId, publicPrivateKey);
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动的材料列表");
        }
        String[] materialList = materialListSysParam.getParamValue().split(",");
        for (String material : materialList) {
            String epayImageType = WechatImageTypeEnum.getEpayImageType(material);
            if (StringUtil.isEmptyOrNull(epayImageType)) {
                rglog.error("特殊费率材料类型<{}>未配置支付平台影像类型.", material);
                throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "特殊费率材料类型未配置支付平台影像类型");
            }
            List<MchntImageInfoBean> mchntImageInfoBeanList = weChatActivityRateRegistRequest.getMchntImageInfoBeanList().stream().filter(item -> epayImageType.equals(item.getImageType())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(mchntImageInfoBeanList) || CollectionUtils.isEmpty(mchntImageInfoBeanList.get(0).getImageInfoList())) {
                rglog.error("缺少影像<{}>.", epayImageType);
                throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "缺少影像:" + epayImageType);
            }

            for (ImageInfoBean imageInfoBean : mchntImageInfoBeanList.get(0).getImageInfoList()) {
                if (StringUtil.isEmptyOrNull(imageInfoBean.getImageLocalPath())) {
                    rglog.error("影像<{}>序号<{}>缺少Path.", epayImageType, imageInfoBean.getImageIndex());
                    throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "影像" + epayImageType + "序号" + imageInfoBean.getImageIndex() + "缺少Path");
                }
            }
        }
    }

    /**
     * 图片上传微信
     */
    private void imageUpToWechat(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest, MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo) throws Exception {
        MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
        List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
        if (CollectionUtils.isEmpty(mchntImageInfoList)) {
            mchntImageInfoList = new ArrayList<>();
        }

        String imageBasePath;
        SysParam imagePathSysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), SysParamEnum.localStorageRootPath);
        if (null != imagePathSysParam && AppParamConstant.YES.equals(imagePathSysParam.getParamStatus())) {
            imageBasePath = imagePathSysParam.getParamValue() + File.separator;
            // imageBasePath = imagePathSysParam.getParamValue() + "/";
        } else {
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }

        MchntImageUnifiedService mchntImageUnifiedService = new MchntImageUnifiedServiceImpl();
        for (MchntImageInfoBean mchntImageInfoBean : weChatActivityRateRegistRequest.getMchntImageInfoBeanList()) {
            for (ImageInfoBean imageInfoBean : mchntImageInfoBean.getImageInfoList()) {
                List<MchntImageInfo> mchntImageInfoOneList = mchntImageInfoList.stream().filter(item ->
                        mchntImageInfoBean.getImageType().equals(item.getImageType()) && imageInfoBean.getImageIndex().equals(String.valueOf(item.getImageIndex()))).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(mchntImageInfoOneList) && !StringUtil.isEmptyOrNull(mchntImageInfoOneList.get(0).getImageLocalPath())
                        && new String(Base64.decodeBase64(imageInfoBean.getImageLocalPath())).equals(mchntImageInfoOneList.get(0).getImageLocalPath())) {
                    rglog.info("影像<{}>序号<{}>未修改,使用原微信图片ID<{}>", mchntImageInfoOneList.get(0).getImageType(), mchntImageInfoOneList.get(0).getImageIndex(), mchntImageInfoOneList.get(0).getWechatMediaId());
                    imageInfoBean.setWechatImageId(mchntImageInfoOneList.get(0).getWechatMediaId());
                } else {
                    String mediaId = mchntImageUnifiedService.uploadToWechat(mchntBaseInfo, wechatMchntInfo, imageBasePath + new String(Base64.decodeBase64(imageInfoBean.getImageLocalPath())));
                    imageInfoBean.setWechatImageId(mediaId);
                }
            }
        }
    }

    /**
     * 保存商户参数
     */
    private void saveMchntParam(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest, MchntBaseInfo mchntBaseInfo) throws Exception {
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();
        try {
            InitParamUtil.savemchntApexInfo(mchntBaseInfo, weChatActivityRateRegistRequest.getMchntApexInfoBeanList());

            MchntImageUnifiedService mchntImageUnifiedService = new MchntImageUnifiedServiceImpl();
            mchntImageUnifiedService.manageMchntImage(mchntBaseInfo, weChatActivityRateRegistRequest.getMchntImageInfoBeanList(), ImageTypeEnum.WECHAT_ACTIVITY_IMAGE_TYPES);

            /*提交事务*/
            dbsUtil.dbsEndTransaction(true);
        } catch (Exception e) {
            rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }
    }

    //region 初始化请求参数

    /**
     * 初始化活动报名请求参数
     */
    private ActivityRateDiscountRegistRequest initActivityRateDiscountRegistRequest(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest,
                                                                                    MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        ActivityRateDiscountRegistRequest activityRateDiscountRegistRequest = new ActivityRateDiscountRegistRequest();
        Header header = InitParamUtil.initWechatHeader("actvratedsctreg", mchntBaseInfo.getInstId(), sysSeqNum);
        activityRateDiscountRegistRequest.setHeader(header);
        activityRateDiscountRegistRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        activityRateDiscountRegistRequest.setThirdMchntNo(wechatMchntInfo.getWechatMchntNo());
        activityRateDiscountRegistRequest.setChannelType(wechatMchntInfo.getDstChannelType());

        ActivityDetailInfo activityDetailInfo = initActivityDetailInfo(weChatActivityRateRegistRequest);
        activityRateDiscountRegistRequest.setActivityDetailInfo(activityDetailInfo);

        ActivityAdditionalInfo activityAdditionalInfo = initActivityAdditionalInfo(weChatActivityRateRegistRequest);
        activityRateDiscountRegistRequest.setActivityAdditionalInfo(activityAdditionalInfo);

        return activityRateDiscountRegistRequest;
    }

    private ActivityDetailInfo initActivityDetailInfo(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest) {
        ActivityDetailInfo activityDetailInfo = new ActivityDetailInfo();
        List<MchntApexInfoBean> activityIdMchntApexInfoBeanList = weChatActivityRateRegistRequest.getMchntApexInfoBeanList()
                .stream().filter(item -> MchntApexPropeytyEnum.WECHAT_ACTIVITY_ID.getPropertyKey().equals(item.getPropertyKey())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(activityIdMchntApexInfoBeanList)) {
            rglog.error("未上传活动ID.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未上传活动ID");
        }
        String activityId = activityIdMchntApexInfoBeanList.get(0).getPropertyValue();
        activityDetailInfo.setActivityId(activityId);
        activityDetailInfo.setActivityRate(weChatActivityRateRegistRequest.getActivityRate());

        List<ActivityApplyInfo> activityApplyInfoList = initActivityApplyInfoList(weChatActivityRateRegistRequest);
        activityDetailInfo.setActivityApplyInfoList(activityApplyInfoList);

        return activityDetailInfo;
    }

    private List<ActivityApplyInfo> initActivityApplyInfoList(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest) {
        List<ActivityApplyInfo> activityApplyInfoList = new ArrayList<>();
        List<MchntImageInfoBean> activityMchntImageInfoBeanList = weChatActivityRateRegistRequest.getMchntImageInfoBeanList().stream().filter(item ->
                !ImageTypeEnum.ADDITIONAL_MATERIAL.getImageType().equals(item.getImageType())).collect(Collectors.toList());
        for (MchntImageInfoBean mchntImageInfoBean : activityMchntImageInfoBeanList) {
            ActivityApplyInfo activityApplyInfo = new ActivityApplyInfo();
            String wechatImageType = WechatImageTypeEnum.getWechatImageType(mchntImageInfoBean.getImageType());
            activityApplyInfo.setApplyMaterialId(wechatImageType);
            List<String> applyMaterialInfo = new ArrayList<>();
            activityApplyInfo.setApplyMaterialInfo(applyMaterialInfo);
            for (ImageInfoBean imageInfoBean : mchntImageInfoBean.getImageInfoList()) {
                applyMaterialInfo.add(imageInfoBean.getWechatImageId());
            }
            activityApplyInfoList.add(activityApplyInfo);
        }
        return activityApplyInfoList;
    }

    private ActivityAdditionalInfo initActivityAdditionalInfo(WeChatActivityRateRegistRequest weChatActivityRateRegistRequest) {
        ActivityAdditionalInfo activityAdditionalInfo = new ActivityAdditionalInfo();
        List<MchntApexInfoBean> additionalMchntApexInfoBeanList = weChatActivityRateRegistRequest.getMchntApexInfoBeanList()
                .stream().filter(item -> MchntApexPropeytyEnum.ADDITIONAL_MSG.getPropertyKey().equals(item.getPropertyKey())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(additionalMchntApexInfoBeanList)) {
            activityAdditionalInfo.setAddtionalMessage(additionalMchntApexInfoBeanList.get(0).getPropertyValue());
        }

        List<MchntImageInfoBean> additionImageInfoBeanList = weChatActivityRateRegistRequest.getMchntImageInfoBeanList().stream().filter(item ->
                ImageTypeEnum.ADDITIONAL_MATERIAL.getImageType().equals(item.getImageType())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(additionImageInfoBeanList)) {
            if (!CollectionUtils.isEmpty(additionImageInfoBeanList.get(0).getImageInfoList())) {
                List<String> addtionalMaterial = new ArrayList<>();
                activityAdditionalInfo.setAddtionalMaterial(addtionalMaterial);
                for (ImageInfoBean imageInfoBean : additionImageInfoBeanList.get(0).getImageInfoList()) {
                    addtionalMaterial.add(imageInfoBean.getWechatImageId());
                }
            }
        }
        return activityAdditionalInfo;
    }
}
